<?php
namespace App\Libraries\Decorator\Post;


use App\Libraries\Decorator\DecoratorInterface\ModelDecorator;
use App\Libraries\Tools\Tools;
use Illuminate\Support\Facades\Auth;

class PostHiddenDecorator implements ModelDecorator
{

    public function before(){
        //todo
    }

    public function after(&$data){
        if(!is_array($data)){
            $data = json_decode(json_encode($data), true);
        }
        if (!empty($data['price']) && !empty(floatval($data['price'])) && empty($data['purchase']) && Auth::id()!=$data['user_id'] && empty(Auth::user()->vip_level)) {
            unset($data['consumed_at']);
            unset($data['shop']);
            unset($data['address']);
            unset($data['longitude']);
            unset($data['latitude']);
            unset($data['contact']);
            unset($data['consumption']);
        } elseif(!empty($data['repost']) && !empty($data['repost']['price']) && !empty(floatval($data['repost']['price'])) && empty($data['repost_purchase']) && Auth::id()!=$data['repost']['user_id']  && empty(Auth::user()->vip_level)){
            unset($data['repost']['consumed_at']);
            unset($data['repost']['shop']);
            unset($data['repost']['address']);
            unset($data['repost']['longitude']);
            unset($data['repost']['latitude']);
            unset($data['repost']['contact']);
            unset($data['repost']['consumption']);
        } else {
            // 多维数组
            if(is_array($data)){
                foreach($data as &$v){
                    $this->after($v);
                }
            }
        }
        return $data;
    }
}